using System;
using System.IO;
using System.Resources;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using newtelligence.DasBlog.Web.Core;

namespace newtelligence.DasBlog.Web
{
    public partial class EditNavigatorLinksBox : UserControl
    {
        private string baseFileName = "navigatorLinks.xml";
        protected NavigationRoot navigationRoot;
        protected ResourceManager resmgr;
        protected RequiredFieldValidator validatorRFname;

        public string BaseFileName
        {
            get { return baseFileName; }
            set { baseFileName = value; }
        }

        private void SaveList(string fileName)
        {
            using (StreamWriter s = new StreamWriter(fileName, false, Encoding.UTF8))
            {
                XmlSerializer ser = new XmlSerializer(typeof (NavigationRoot));
                ser.Serialize(s, navigationRoot);
            }
        }

        private void LoadList(string fileName)
        {
            if (File.Exists(fileName))
            {
                using (Stream s = File.OpenRead(fileName))
                {
                    XmlSerializer ser = new XmlSerializer(typeof (NavigationRoot));
                    navigationRoot = (NavigationRoot) ser.Deserialize(s);
                }
            }
            else
            {
                navigationRoot = new NavigationRoot();
            }

            Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] = navigationRoot;
        }

        private void BindGrid()
        {
            navigatorLinksGrid.DataSource = navigationRoot.Items;
            navigatorLinksGrid.Columns[0].HeaderText = resmgr.GetString("text_navigatorlinks_title");
            DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (SiteSecurity.IsInRole("admin") == false)
            {
                Response.Redirect("~/FormatPage.aspx?path=SiteConfig/accessdenied.format.html");
            }

            resmgr = ApplicationResourceTable.Get();

            if (!IsPostBack ||
                Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] == null)
            {
                string fileName = Path.Combine(SiteConfig.GetConfigPathFromCurrentContext(), baseFileName);
                LoadList(fileName);
            }
            else
            {
                navigationRoot =
                    Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] as NavigationRoot;
            }
            BindGrid();
        }

        private void navigatorLinksGrid_EditCommand(object source, DataGridCommandEventArgs e)
        {
            navigatorLinksGrid.EditItemIndex = e.Item.ItemIndex;
            navigatorLinksGrid.DataBind();
        }

        private void navigatorLinksGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            navigationRoot.Items.RemoveAt(e.Item.DataSetIndex);
            SaveList(Path.Combine(SiteConfig.GetConfigPathFromCurrentContext(), baseFileName));
            Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] = null;

            Response.Redirect(Page.Request.Url.AbsoluteUri, true);
        }

        private void navigatorLinksGrid_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            navigatorLinksGrid.EditItemIndex = -1;
            navigatorLinksGrid.DataBind();
        }

        private void navigatorLinksGrid_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            NavigationLink link = navigationRoot.Items[e.Item.DataSetIndex];
            link.Name = ((TextBox) e.Item.FindControl("textname")).Text;
            link.Url = ((TextBox) e.Item.FindControl("texturl")).Text;

            SaveList(Path.Combine(SiteConfig.GetConfigPathFromCurrentContext(), baseFileName));
            Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] = null;

            Response.Redirect(Page.Request.Url.AbsoluteUri, true);
        }

        private void navigatorLinksGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            navigatorLinksGrid.CurrentPageIndex = e.NewPageIndex;
            navigatorLinksGrid.DataBind();
        }

        private void navigatorLinksGrid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "AddItem")
            {
                NavigationLink newLink = new NavigationLink();
                newLink.Name = "New Entry";
                navigationRoot.Items.Insert(0, newLink);
                navigatorLinksGrid.CurrentPageIndex = 0;
                navigatorLinksGrid.EditItemIndex = 0;
                BindGrid();
            }
            else if (e.CommandName == "MoveUp" && e.Item != null)
            {
                int position = e.Item.DataSetIndex;

                if (position > 0)
                {
                    NavigationLink link = new NavigationLink();
                    link.Name = navigationRoot.Items[position].Name;
                    link.Url = navigationRoot.Items[position].Url;
                    navigationRoot.Items.RemoveAt(position);
                    navigationRoot.Items.Insert(position - 1, link);
                    navigatorLinksGrid.CurrentPageIndex = (position - 1)/navigatorLinksGrid.PageSize;
                    if (navigatorLinksGrid.EditItemIndex == position)
                    {
                        navigatorLinksGrid.EditItemIndex = position - 1;
                    }
                    else if (navigatorLinksGrid.EditItemIndex == position - 1)
                    {
                        navigatorLinksGrid.EditItemIndex = position;
                    }
                }
                SaveList(Path.Combine(SiteConfig.GetConfigPathFromCurrentContext(), baseFileName));
                Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] = null;

                Response.Redirect(Page.Request.Url.AbsoluteUri, true);
            }
            else if (e.CommandName == "MoveDown" && e.Item != null)
            {
                int position = e.Item.DataSetIndex;

                if (position < navigationRoot.Items.Count - 1)
                {
                    NavigationLink link = new NavigationLink();
                    link.Name = navigationRoot.Items[position].Name;
                    link.Url = navigationRoot.Items[position].Url;
                    navigationRoot.Items.RemoveAt(position);
                    navigationRoot.Items.Insert(position + 1, link);
                    navigatorLinksGrid.CurrentPageIndex = (position + 1)/navigatorLinksGrid.PageSize;
                    if (navigatorLinksGrid.EditItemIndex == position)
                    {
                        navigatorLinksGrid.EditItemIndex = position + 1;
                    }
                    else if (navigatorLinksGrid.EditItemIndex == position + 1)
                    {
                        navigatorLinksGrid.EditItemIndex = position;
                    }
                }
                SaveList(Path.Combine(SiteConfig.GetConfigPathFromCurrentContext(), baseFileName));
                Session["newtelligence.DasBlog.Web.EditNavigatorLinksBox.NavigationRoot"] = null;

                Response.Redirect(Page.Request.Url.AbsoluteUri, true);
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        
        private void InitializeComponent()
        {
            this.navigatorLinksGrid.ItemCommand +=
                new System.Web.UI.WebControls.DataGridCommandEventHandler(this.navigatorLinksGrid_ItemCommand);
            this.navigatorLinksGrid.PageIndexChanged +=
                new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.navigatorLinksGrid_PageIndexChanged);
            this.navigatorLinksGrid.CancelCommand +=
                new System.Web.UI.WebControls.DataGridCommandEventHandler(this.navigatorLinksGrid_CancelCommand);
            this.navigatorLinksGrid.EditCommand +=
                new System.Web.UI.WebControls.DataGridCommandEventHandler(this.navigatorLinksGrid_EditCommand);
            this.navigatorLinksGrid.UpdateCommand +=
                new System.Web.UI.WebControls.DataGridCommandEventHandler(this.navigatorLinksGrid_UpdateCommand);
            this.navigatorLinksGrid.DeleteCommand +=
                new System.Web.UI.WebControls.DataGridCommandEventHandler(this.navigatorLinksGrid_DeleteCommand);
        }

        #endregion
    }
}